<?php

/**
 * This is the model class for table "branch".
 *
 * The followings are the available columns in table 'branch':
 * @property string $id
 * @property string $name
 * @property string $address
 * @property string $phone
 * @property string $latlng
 * @property string $status
 * @property integer $index
 *
 * The followings are the available model relations:
 * @property Bill[] $bills
 * @property Manager[] $managers
 */
class Branch extends Model
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Branch the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'branch';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, address, latlng, index', 'required'),
			array('index', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>50),
			array('address, phone', 'length', 'max'=>255),
			array('latlng', 'length', 'max'=>30),
			array('status', 'length', 'max'=>9),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, address, phone, latlng, status, index', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bills' => array(self::HAS_MANY, 'Bill', 'branch_id'),
			'managers' => array(self::HAS_MANY, 'Manager', 'branch_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'address' => 'Address',
			'phone' => 'Phone',
			'latlng' => 'Latlng',
			'status' => 'Status',
			'index' => 'Index',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('latlng',$this->latlng,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('index',$this->index);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function getBranchData(){
        $branchs = array(); 
        $command = Yii::app()->db->createCommand("SELECT id, `name`, address FROM branch WHERE `status` = 'ENABLE' ORDER BY `index` ASC");
        $dataReader = $command->query();
        while($row = $dataReader->read()){
            $branchs[$row['id']] = $row['name'].' - '. $row['address'];
        }
        return $branchs;
    }
    
    public function getBranchLabel(){
        return $this->branchData[$this->id];
    }
    
    public function getLat(){
        $ll = explode(',', $this->latlng);
        return current($ll);
    }
    public function getLng(){
        $ll = explode(',', $this->latlng);
        return end($ll);
    }
    
}